package top.hhydra.mimall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.hhydra.mimall.entity.Product;
import top.hhydra.mimall.enums.ProductStateEnum;
import top.hhydra.mimall.enums.ResponseEnum;
import top.hhydra.mimall.mapper.ProductMapper;
import top.hhydra.mimall.service.ICategoryService;
import top.hhydra.mimall.service.IProductService;
import top.hhydra.mimall.vo.ProductDetailVo;
import top.hhydra.mimall.vo.ProductVo;
import top.hhydra.mimall.vo.ResponseVo;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author SaiyaEmon
 * @since 2020-02-11
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    ICategoryService categoryService;

    /**
     * 分页查询商品
     * @param pageNum
     * @param pageSize
     * @param categoryId
     * @return
     */
    @Override
    public ResponseVo<IPage<ProductVo>> products(Integer pageNum, Integer pageSize, Integer categoryId) {
        List<Integer> categoryIds = new ArrayList<>();
        categoryService.findSubCategoryId(categoryId, categoryIds);
        categoryIds.add(categoryId);

        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("category_id", categoryIds);
        IPage<Product> productPage = new Page<>(pageNum, pageSize);
        productPage = baseMapper.selectPage(productPage, queryWrapper);
        IPage<ProductVo> productVoPage = new Page<>();
        BeanUtils.copyProperties(productPage, productVoPage);
        productVoPage.setRecords(productPage.getRecords().stream().map(e -> {
            ProductVo productVo = new ProductVo();
            BeanUtils.copyProperties(e, productVo);
            return productVo;
        }).collect(Collectors.toList()));

        return ResponseVo.success(productVoPage);
    }

    /**
     * 查询商品详情
     *
     * @param productId
     * @return
     */
    @Override
    public ResponseVo<ProductDetailVo> product(Integer productId) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", productId);
        Product product = baseMapper.selectOne(queryWrapper);
        if (product == null
                || product.getStatus().equals(ProductStateEnum.OFF_SELL.getCode())
                || product.getStatus().equals(ProductStateEnum.DELETE.getCode())) {
            return ResponseVo.error(ResponseEnum.HAVE_NOT_PRODUCT);
        }

        ProductDetailVo productDetailVo = new ProductDetailVo();
        BeanUtils.copyProperties(product, productDetailVo);
        return ResponseVo.success(productDetailVo);
    }
}
